package com.haitaoit.chengquan.module.center_service.activity.xuqiu_order;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.module.center.activity.xuqiu.XuqiuEvaluateActivity;
import com.haitaoit.chengquan.module.center_service.model.ServiceXuqiuDetailModel;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.CircleImageView;
import com.haitaoit.chengquan.view.QRatingBar;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ServiceXuqiuDetailActivity extends BaseActivity {
    @ViewInject(R.id.ser_detail_evaLL)
    LinearLayout ser_detail_evaLL;
    @ViewInject(R.id.ser_detail_reasonll)
    LinearLayout ser_detail_reasonll;
    @ViewInject(R.id.ser_detail_bottomll)
    LinearLayout ser_detail_bottomll;
    @ViewInject(R.id.ser_detail_state)
    TextView ser_detail_state;
    @ViewInject(R.id.ser_baojia_state)
    ImageView ser_baojia_state;
    @ViewInject(R.id.title_img)
    ImageView title_img;
    @ViewInject(R.id.demand_number)
    TextView demand_number;
    @ViewInject(R.id.detail_title)
    TextView detail_title;
    @ViewInject(R.id.detail_type)
    TextView detail_type;
    @ViewInject(R.id.contact)
    TextView contact;
    @ViewInject(R.id.offer_money)
    TextView offer_money;
    @ViewInject(R.id.addtime)
    TextView addtime;
    @ViewInject(R.id.contact_information)
    TextView contact_information;
    @ViewInject(R.id.remarks)
    TextView remarks;
    @ViewInject(R.id.head_portrait)
    CircleImageView head_portrait;
    @ViewInject(R.id.eva_name)
    TextView eva_name;
    @ViewInject(R.id.eva_content)
    TextView eva_content;
    @ViewInject(R.id.eva_addtime)
    TextView eva_addtime;
    @ViewInject(R.id.eva_star)
    QRatingBar eva_star;


    private String id;
    private ServiceXuqiuDetailModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_service_xuqiu_detail);
        setTitle_V("订单详情");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


        id = getIntent().getStringExtra("order_id");
    }

    @Override
    protected void onResume() {
        httpGetmer_demand_model();
        super.onResume();
    }

    @OnClick({R.id.ser_to_eva,})
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ser_to_eva:
                intent.setClass(ServiceXuqiuDetailActivity.this, XuqiuEvaluateActivity.class);
                intent.putExtra("demand_id", model.getId());
                intent.putExtra("type", "2");
                intent.putExtra("circleimage_uri", model.getEvaluation_model().getHead_portrait());
                startActivity(intent);
                break;
        }
        super.onClick(v);
    }

    private void httpGetmer_demand_model() {
        List<NameValuePairSign> list = new ArrayList<NameValuePairSign>();
        list.add(new NameValuePairSign("id", id));
        String sign = GetSign.genPackageSign(list);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("id", id);
        params.addQueryStringParameter("sign", sign);

        HttpUtilsSingle.doGet(this, false, Constant.Getmer_demand_model, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String result = arg0.result.toString();
                try {
                    JSONObject json = new JSONObject(result);
                    String Errcode = json.getString("ErrCode");
                    String Errmsg = json.getString("ErrMsg");
                    String Response = json.getString("Response");
                    Gson gson = new Gson();
                    switch (Integer.valueOf(Errcode)) {
                        case 1:// 失败
                            ToastUtils.showToast(Errmsg);
                            break;
                        case 0:// 成功
                            model = gson.fromJson(Response, ServiceXuqiuDetailModel.class);
                            // 订单状态(1报价成功2报价失败3已支付4已完成)
                            if (model.getOrder_status().equals("1")) {
                                ser_detail_evaLL.setVisibility(View.GONE);
                                ser_detail_reasonll.setVisibility(View.GONE);
                                ser_detail_bottomll.setVisibility(View.GONE);
                                ser_baojia_state.setVisibility(View.GONE);
                                ser_detail_state.setText("已报价");
                            } else if (model.getOrder_status().equals("2")) {
                                ser_detail_evaLL.setVisibility(View.GONE);
                                ser_detail_bottomll.setVisibility(View.GONE);
                                ser_detail_reasonll.setVisibility(View.VISIBLE);
                                ser_baojia_state.setVisibility(View.VISIBLE);
                                ser_baojia_state.setImageResource(R.mipmap.pic30);
                                ser_detail_state.setText("报价失败");
                            } else if (model.getOrder_status().equals("3")) {
                                ser_detail_evaLL.setVisibility(View.GONE);
                                ser_detail_reasonll.setVisibility(View.GONE);
                                ser_detail_bottomll.setVisibility(View.GONE);
                                ser_baojia_state.setVisibility(View.VISIBLE);
                                ser_baojia_state.setImageResource(R.mipmap.img58);
                                ser_detail_state.setText("已支付");
                            } else if (model.getOrder_status().equals("4")) {
                                ser_detail_reasonll.setVisibility(View.GONE);
                                ser_baojia_state.setVisibility(View.GONE);
                                ser_baojia_state.setImageResource(R.mipmap.img58);

                                // is_evaluation_mer:服务商是否评价1否2是
                                if (model.getIs_evaluation_mer().equals("1")) {
                                    ser_detail_bottomll.setVisibility(View.VISIBLE);
                                } else {
                                    ser_detail_bottomll.setVisibility(View.GONE);
                                }
                                // view_evaluation:1不显示需求商对我的评价2显示需求商对我的评价
                                if (model.getView_evaluation().equals("1")) {
                                    ser_detail_evaLL.setVisibility(View.GONE);
                                } else {
                                    ser_detail_evaLL.setVisibility(View.VISIBLE);
                                    ImageLoader.getInstance().displayImage(model.getEvaluation_model().getHead_portrait(), head_portrait);
                                    eva_name.setText(model.getEvaluation_model().getName());
                                    eva_content.setText(model.getEvaluation_model().getContent());
                                    eva_addtime.setText(model.getEvaluation_model().getAddtime());
                                    eva_star.setRating(Float.parseFloat(model.getEvaluation_model().getScore()));
                                }
                                ser_detail_state.setText("已完成");
                            }

                            demand_number.setText("需求编号：" + model.getDemand_number());
                            ImageLoader.getInstance().displayImage(model.getTitle_img(), title_img);
                            detail_title.setText(model.getTitle());
                            detail_type.setText("分类：" + model.getType());
                            contact.setText("需求方：" + model.getContact());
                            offer_money.setText("¥ " + model.getOffer_money());
                            addtime.setText("报价时间：" + model.getAddtime());
                            contact_information.setText(model.getContact_information());
                            if (model.getRemarks().equals(""))
                                remarks.setText("无");
                            else
                                remarks.setText(model.getRemarks());
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                ToastUtils.showToast("获取数据失败,请检查网络");
            }
        });
    }
}
